let app = getApp()
Component({
    properties: {
         orderid:Number
      },
      data:{
        services_id: "",
        detail: {},
        form_item: [],
        time_arr: [],
        time_picker_arr: [],
        time_index: [ 0, 0, 0, 0 ],
        time_value: "",
        time_placeholder: "请选择预约时间",
        page_path: "pages/services/order/create/index",
        basicArr: {},
        yearIndex: 0,
        is_alpay: !1,
        years: [ "2019" ],
        months: [ "01" ],
        days: [ "01" ],
        hours: [ "01" ],
        minutes: [ "01" ],
        picker_view_value: [],
        is_minutes: !0,
        activePayType: 0,
        payTypeList: [],
        formAuthPhoneStatus: !0,
        is_time:2
      },
    lifetimes: {
        attached: function () {
          var that=this
          that.setData({
            globalColor:app.globalData.globalColor
          })
           wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Services_getServices',
                id:that.data.orderid
              },
              success: function(res) {
                  that.setData({
                    detail:res.data.info,
                    payTypeList:res.data.info.canUsePayList
                  })
              }
            })
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Services_getTime',
                services_id:that.data.orderid
              },
              success: function(res) {
                  that.setData({
                    time_arr:res.data.info.time_arr, 
                    time_picker_arr:res.data.info.time_picker_arr, 
                  })
              }
            })
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Services_getFormItem',
                id:that.data.orderid
              },
              success: function(res) {
                var form_item=res.data.info
                form_item=form_item.concat([ {
                  default: 1,
                  name: "pay_type",
                  required: 1,
                  show: 1,
                  size: 1,
                  title: "支付方式",
                  type: "pay_type"
                 }])
                  that.setData({
                    form_item:form_item
                  })
              }
            })
       }
      },
    methods: {
      timechange:function(e){
        var time_arr=this.data.time_arr
        var years=time_arr[0][e.detail.value[0]]
        var months=time_arr[1][e.detail.value[1]]
        var days=time_arr[2][e.detail.value[2]]
        var minutes=time_arr[3][e.detail.value[3]]
        var time_value=years+'-'+months+'-'+days+' '+minutes
        if(typeof(years)!='undefined'){
          this.setData({
            time_value:time_value
          })
        }
      },
      valuechange:function(e){
        if(typeof(e.detail.value)!='undefined')
        {
          var form_item=this.data.form_item
          form_item[e.currentTarget.dataset.index]['value']=e.detail.value
          this.setData({
            form_item:form_item
          })
        }
      },
      verification:function(){
        var form_item=this.data.form_item
       if(this.data.is_time!=2){
        if(typeof(this.data.time_value)=='undefined'||this.data.time_value==''){
          wx.showModal({
            title: "提示",
            showCancel: !1,
            content:'预约时间不能为空'
           })
          return false
        }else{
          for(var i=0;i<form_item.length; i++){
            if(form_item[i]['required']==1&&form_item[i]['show']==1&&form_item[i]['name']!=='pay_type'){
              if(typeof(form_item[i]['value'])=='undefined'||form_item[i]['value']==''){
                wx.showModal({
                  title: "提示",
                  showCancel: !1,
                  content:form_item[i]['title']+ "不能为空！"
                 })
                return false
              }
              else if(form_item[i]['name']=='tel'){
                 var  myreg=/^1\d{10}$/
                 if (!myreg.test(form_item[i]['value'])) {
                  wx.showModal({
                    title: "提示",
                    showCancel: !1,
                    content:"手机号格式不正确"
                   })
                  return false
                 }
              }
            }
          }
        }
       }else{
        for(var i=0;i<form_item.length; i++){
          if(form_item[i]['required']==1&&form_item[i]['show']==1&&form_item[i]['name']!=='pay_type'){
            if(typeof(form_item[i]['value'])=='undefined'||form_item[i]['value']==''){
              wx.showModal({
                title: "提示",
                showCancel: !1,
                content:form_item[i]['title']+ "不能为空！"
               })
              return false
            }else if(form_item[i]['name']=='tel'){
              var  myreg=/^1\d{10}$/
              if (!myreg.test(form_item[i]['value'])) {
               wx.showModal({
                 title: "提示",
                 showCancel: !1,
                 content:"手机号格式不正确"
                })
               return false
              }
           }
          }
        }
       }
        return true
      },
      submitorder:function(){
        var form_item=this.data.form_item
        var that=this
        if(this.verification()){
          var pay_type=that.data.payTypeList[that.data.activePayType].value!=null?that.data.payTypeList[that.data.activePayType].value:''
          console.log(this.data.time_value)
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Services_createOrder',
              services_id:that.data.orderid,
              user_id:wx.getStorageSync('user_id'),
              form_info:that.data.form_item,
              services_time:that.data.time_value,
              pay_type:pay_type,
              times:this.data.time_value
            },
            success: function(res) {
               if(res.data.code==0){
                wx.showModal({
                  title: "提示",
                  showCancel: !1,
                  content:res.data.msg
                 })
               }else{
                wx.showToast({
                  title:res.data.msg,
                  icon: 'none',
                  duration: 1500
                })
                 wx.redirectTo({
                  url: '/yb_shopv2/native/all/index?type=pay_result&order_type=100',
                 }) 
               }
             
            }
          })
        }
      }
    }
});
